package com.by.springboottwodatabases.controller;

import com.by.springboottwodatabases.controller.request.OneRequestBean;
import com.by.springboottwodatabases.controller.request.TestRequestBean;
import com.by.springboottwodatabases.domain.one.DOne;
import com.by.springboottwodatabases.domain.one.DTestOne;
import com.by.springboottwodatabases.service.OneService;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping(value = "/one")
public class OneController {

    private final OneService oneService;

    public OneController(OneService oneService) {
        this.oneService = oneService;
    }

    @RequestMapping(value = "", method = RequestMethod.POST)
    public DOne createOne(@RequestBody OneRequestBean bean, HttpServletRequest request) {
        return oneService.createOne(bean);
    }

    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public DOne getOne(@PathVariable Long id, HttpServletRequest request) {
        return oneService.findById(id);
    }

    @RequestMapping(value = "/{id}", method = RequestMethod.PUT)
    public DOne updateOne(@PathVariable Long id, @RequestBody OneRequestBean bean, HttpServletRequest request) {
        return oneService.updateOne(id, bean);
    }

    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    public void deleteOne(@PathVariable Long id, HttpServletRequest request) {
        oneService.deleteOne(id);
    }


    @RequestMapping(value = "/test", method = RequestMethod.POST)
    public DTestOne createTest(@RequestBody TestRequestBean bean, HttpServletRequest request) {
        return oneService.createTest(bean);
    }

    @RequestMapping(value = "/test/{id}", method = RequestMethod.GET)
    public DTestOne getTest(@PathVariable Long id, HttpServletRequest request) {
        return oneService.findTestById(id);
    }

    @RequestMapping(value = "/test/{id}", method = RequestMethod.PUT)
    public DTestOne updateTest(@PathVariable Long id, @RequestBody TestRequestBean bean, HttpServletRequest request) {
        return oneService.updateTest(id, bean);
    }

    @RequestMapping(value = "/test/{id}", method = RequestMethod.DELETE)
    public void deleteTest(@PathVariable Long id, HttpServletRequest request) {
        oneService.deleteTest(id);
    }

}
